package cn.edu.qfnu.oj.dao.impl;

import cn.edu.qfnu.common.dao.BaseDaoHib;
import cn.edu.qfnu.oj.dao.ClassDao;
import cn.edu.qfnu.oj.entity.Classes;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * OnlineJudge of QF_NU
 * Created by LS on 2017/7/17.
 */
@Repository("classDao")
public class ClassDaoImpl extends BaseDaoHib<Classes, Integer> implements ClassDao {

    @Override
    public Classes isRepeat(Integer classNumber, String classType, Integer grade) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Classes.class);
        detachedCriteria.add(Restrictions.eq("classNumber", classNumber));
        detachedCriteria.add(Restrictions.eq("classType", classType));
        detachedCriteria.add(Restrictions.eq("grade", grade));

        Criteria criteria = detachedCriteria.getExecutableCriteria(currentSession());

        Classes result = (Classes) criteria.uniqueResult();

        return result != null ? result : new Classes(classNumber, grade, classType);
    }
}
